package top.aiprogram.user.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import top.aiprogram.user.entity.Student;
import top.aiprogram.user.service.StudentService;


import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2017/7/18.
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private StudentService studentService;


    @RequestMapping(value = "/test")
    public String testUser(@RequestBody Student student ){

        studentService.createStudent(student);

        return "user";
    }

    @RequestMapping("/demo")
    public String demoUser(){

        return "demo";
    }

    @RequestMapping("/upload")
    public String upload(MultipartFile file,HttpServletRequest request){

        String name = file.getOriginalFilename();
        File uploadF = new File("/"+name);

        try {
            file.transferTo(uploadF);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "demo";
    }

    @RequestMapping("/down")
    public ResponseEntity<byte[]> download() throws IOException {
        String path = "D:/1.png";
        File file = new File(path);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment",path);
        headers.setContentType(MediaType.IMAGE_PNG);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
    }

}
